package cn.fenqing168.springcloud.controller;

import cn.fenqing168.springcloud.commons.BaseResult;
import cn.fenqing168.springcloud.entities.Payment;
import cn.fenqing168.springcloud.lb.LoadBalancer;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@Slf4j
public class OrderController {

    public final static String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    public RestTemplate restTemplate;

    @Autowired
    private LoadBalancer loadBalancer;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/consumer/payment/create")
    public BaseResult<Integer> create(@RequestBody Payment payment){
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<>(JSONObject.toJSONString(payment), headers);
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", formEntity, BaseResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public BaseResult<Payment> get(@PathVariable Long id){
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, BaseResult.class);
    }

    @GetMapping("/consumer/payment/get2/{id}")
    public BaseResult<Payment> get2(@PathVariable Long id){
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        ResponseEntity<BaseResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, BaseResult.class);
        if(forEntity.getStatusCode().is2xxSuccessful()){
            return forEntity.getBody();
        }
        return BaseResult.error();
    }

    @GetMapping("/consumer/payment/Lb")
    public String getPaymentLb(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if(CollectionUtil.isNotEmpty(instances)){
            ServiceInstance instances1 = loadBalancer.instances(instances);
            URI uri = instances1.getUri();
            return restTemplate.getForObject(uri + "payment/lb", String.class);
        }
        throw new RuntimeException("失败");
    }

    @GetMapping("/consumer/payment/zipkin")
    public String zipkin(){
        return restTemplate.getForObject(PAYMENT_URL + "/payment/zipkin", String.class);
    }

}
